import 'package:flutter/material.dart';
import 'package:mall/constant/app_dimens.dart';
import 'package:mall/constant/app_strings.dart';
import 'package:mall/constant/text_style.dart';
import 'package:mall/modules/model/home_entity.dart';
import 'package:mall/modules/service/mine_service.dart';
import 'package:mall/modules/service/user_service.dart';
import 'package:mall/router/navigator_util.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mall/utils/hud/hud_util.dart';

class TabHomeCoupon extends StatelessWidget {
  final List<HomeModelCouponlist> _homeModelCouponlist;

  TabHomeCoupon(this._homeModelCouponlist);

  @override
  Widget build(BuildContext context) {
    return _homeModelCouponlist.length == 0
        ? Container()
        : Column(
            children: [
              Container(
                margin: EdgeInsets.only(left: 15, top: 15, bottom: ScreenUtil().setHeight(AppDimens.DIMENS_10)),
                alignment: Alignment.centerLeft,
                child: Text(AppStrings.COUPON_AREA, style: FMTextStyle.color_333333_size_13_bold),
              ),
              ListView.builder(
                  padding: EdgeInsets.all(0),
                  physics: NeverScrollableScrollPhysics(),
                  //禁止滑动
                  shrinkWrap: true,
                  //Vertical viewport was given unbounded height.  Column 中且套GridView、ListView的问题
                  itemCount: _homeModelCouponlist.length,
                  itemBuilder: (BuildContext context, int index) {
                    return _getCouponView(_homeModelCouponlist[index], context);
                  })
            ],
          );
  }

  Widget _getCouponView(HomeModelCouponlist couponEntity, BuildContext context) {
    return Container(
        child: InkWell(
      onTap: () => _receiveCoupon(couponEntity.id, context),
      child: Card(
        margin: EdgeInsets.all(15),
        child: Container(
          margin: EdgeInsets.only(top: 10, bottom: 10),
          child: Row(
            children: <Widget>[
              Container(
                alignment: Alignment.center,
                width: 65,
                child:
                    Text("${couponEntity.discount}${AppStrings.MONEY_UNIT}", style: FMTextStyle.color_ff5722_size_22),
              ),
              Expanded(
                child: Container(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Text(couponEntity.name ?? "", style: FMTextStyle.color_222222_size_13),
                      Padding(
                        padding: EdgeInsets.only(top: 10),
                      ),
                      Text("${AppStrings.FULL}${couponEntity.min}${AppStrings.USE}",
                          style: FMTextStyle.color_999999_size_13),
                      Padding(
                        padding: EdgeInsets.only(top: 10),
                      ),
                      Text("${AppStrings.VALIDITY}${couponEntity.days}${AppStrings.DAY}",
                          style: FMTextStyle.color_999999_size_13),
                    ],
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    ));
  }

  _receiveCoupon(int? id, BuildContext context) async {
    if (id == null) {
      return;
    }
    final isLogin = await UserService.isLogin();
    if (!isLogin) {
      NavigatorUtil.goLogin();
      return;
    }

    MineService.receiveCoupon(id).then((response) {
      HUDUtil.toast(AppStrings.RECEIVE_COUPON_SUCCESS);
    });

    // SharedPreferencesUtil.getInstance()
    //     .getString(AppStrings.TOKEN)
    //     .then((response) {
    //   if (response != null) {
    //     _tabHomeViewModel.receiveCoupon(id).then((response) {
    //       if (response) {
    //         HUDUtil.toast(AppStrings.RECEIVE_COUPON_SUCCESS);
    //       }
    //     });
    //   } else {
    //     NavigatorUtil.goLogin(context);
    //   }
    // });
  }
}
